package com.gljx.web.service.impl;

import com.gljx.constant.ActivitiKey;
import com.gljx.util.ActitiviUtil;
import com.gljx.util.ServerResponse;
import com.gljx.web.Entity.ChangeClazz;
import com.gljx.web.Entity.Student;
import com.gljx.web.dao.ChangeClazzMapper;
import com.gljx.web.dao.CommonDao;
import com.gljx.web.dao.StudentMapper;
import com.gljx.web.service.ChangeClazzService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

@Service
@Transactional
public class ChangeClazzServiceImpl extends CommonDao implements ChangeClazzService  {
	
	@Autowired
	private ChangeClazzMapper changeClazzMapper;

	@Autowired
	private StudentMapper studentMapper;

	//导入activiti工具
	ActitiviUtil activitiUtil = new ActitiviUtil();

	//1.提交
	//用户点击提交按钮时我们将流程创建出来
	public void 	submitFlow(ChangeClazz changeClazz) {
		//1.这里是你修改单据的代码，此处你应该有一个单据


		//2.将这个单据编号和你需要保存的流程变量存入map集合中
		Map<String, Object> map = new HashMap<String, Object>();

		map.put(ActivitiKey.PUBLIC_PROCESS, changeClazz.getFnumber());//使用你自己单据的key，或者用默认的
		/*com.gljx.constant.ActivitiKey 中存入流程相关的常量，最好将流程定义的key也存入其中*/

		//2.查询
		String result = activitiUtil.getTaskStateByUser(
				ActivitiKey.PUBLIC_PROCESS, changeClazz.getFnumber()               //单据
		);
		if (activitiUtil.notTask.equals(result)) {
			System.out.println("该单据没有对应流程");
			//3.调用方法
			String piID = activitiUtil.creatFlow(ActivitiKey.PUBLIC_PROCESS, map);
			activitiUtil.completeFlowByPiID(piID);
		} else if(activitiUtil.FINISH.equals(result)){
				System.out.println("该单据没有对应流程");
				//3.调用方法
				String piID = activitiUtil.creatFlow(ActivitiKey.PUBLIC_PROCESS, map);
				activitiUtil.completeFlowByPiID(piID);
		}else  {
			System.out.println("当前用户可以审核该单据");
			activitiUtil.complteFlowByVariable(ActivitiKey.PUBLIC_PROCESS, changeClazz.getFnumber());
		}
	}

	@Override
	public String checkChangeClazz(ChangeClazz changeClazz, String state, String agreed, String auditcomment) {
		if ("5".equalsIgnoreCase(state)) {
			if(auditcomment == null || auditcomment.equals(""))
				auditcomment="同意";
			return checkFlow(changeClazz, agreed, auditcomment);
		}
		if ("3".equalsIgnoreCase(state)) {
			uncheckFlow(changeClazz,auditcomment);
			changeClazz.setCfbillstate(Long.valueOf("3"));
			changeClazz.setFsourcefunction(auditcomment);
			changeClazzMapper.updateByPrimaryKeySelective(changeClazz);
			return "反审核成功！";
		}
		return null;
	}


	//2.审核
	//用户点击审核按钮时的操作
	public String checkFlow(ChangeClazz changeClazz, String agreed, String auditcomment) {
		//1.获取页面传来的单据ID

		//2.根据单据来推进指定流程

		//2.1审核有两种情况同意与不同意，不同意可能有批注


		//2.2使用这个方法必须保证usertask节点与其子连线key值符合要求
		String returnMsg =
				activitiUtil
						.auditComplte(
								ActivitiKey.PUBLIC_PROCESS,
								changeClazz.getFnumber(),                  //单据
								Integer.valueOf(agreed),                 //1、0是否同意取决于你的流程图
								auditcomment              //批注，直接把前端传过来的string塞进来
						);
		if(activitiUtil.notAssign .equals(returnMsg)) {
			return  "当前用户不是要求的经办人";
		} else if(activitiUtil.FINISH.equals(returnMsg)) {
			changeClazz.setCfbillstate((long) 5);
			Student student = new Student();                                    //审核通过后，修改学员信息
					student.setCfclazzid(changeClazz.getCfzrbjid());
					student.setCfamount(changeClazz.getCfbjje());
					student.setFid(changeClazz.getCfstudentid());
					student.setCfarrearage(changeClazz.getCfbjje().subtract(changeClazz.getCfamount()));
			studentMapper.updateByPrimaryKeySelective(student);
			changeClazzMapper.updateByPrimaryKeySelective(changeClazz);
			return  "此流程成功推进，并且此流程已经结束";
		} else if(activitiUtil.SUCCESS.equals(returnMsg)) {
			return  "此流程成功推进";
		} else  {
			return "没有找到正在进行的相关流程";
		}

	}

	//3.反审核
	//用户点击审核按钮时的操作
	public ServerResponse uncheckFlow(ChangeClazz changeClazz,String message) {
		//需要注意反审核按钮是位于流程之外只与权限关联的。
		//这里统一跳提交按钮处
		//同样的流程图需要符合规范

		//1.获取页面传来的单据ID
		/*String danju = "YHTDF202165416531321";*/

		Object result = activitiUtil.jumpActivtiMessage(
				ActivitiKey.PUBLIC_PROCESS,
				changeClazz.getFnumber()  ,                  //
				message
		);
		if (activitiUtil.SUCCESS.equals(result)) {
			System.out.println("反审核成功！");
			return ServerResponse.createBySuccessMessage("反审核成功！");
		} else if (activitiUtil.notActiviti.equals(result)) {
			System.out.println("找不到流程节点");
			return ServerResponse.createByErrorMessage("找不到流程节点");

		} else {
			System.out.println("没有找到任务");
			return ServerResponse.createByErrorMessage("没有找到任务");
		}
	}

	//4.查询流程状态
	//进入详情页面调用
	public String checkActiviti(ChangeClazz changeClazz) {
		//1.获取页面传来的单据ID
		/*String danju = "YHTDF202165416531321";*/

		//2.查询
		String result = activitiUtil.getTaskStateByUser(
				ActivitiKey.FNUMBER,
				changeClazz.getFnumber()                  //单据
		);

		if(activitiUtil.notTask.equals(result))
		{
			return "1";  //没有历史流程（从来没有过流程的“新单据”）
		}
		else if(activitiUtil.FINISH.equals(result))
		{
			return "2";          //只有有历史流程
		}//剩下两种是该任务进行中
		else if(activitiUtil.notAssign.equals(result))
		{
			return "3" ;              //正在进行的流程，但是经办人跟当前用户不匹配
		}
		else if(activitiUtil.ISPOWER.equals(result))
		{
			return "4" ;     /**正在进行的流程，且当前用户能够审核
		 * 如果是进入可提交的详情页面，这个标记表示
		 * 此流程进行中，即用户点击提交只需推进流程即可
		 */
		}
		return null;
	}


	/**
     * 取分页数据    dmz 20160921
     *
     * @param pindex 页码
     * @param psql   SQL语句，需要带 rownum rn 例如：select rownum rn,t.* from zzda t
     * @param psize  每页记录数
     */
    @Override
    public Map<String, Object> selectPage(int pindex, String psql, int psize) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("Pindex", pindex);
        paramMap.put("Psql", psql);
        paramMap.put("Psize", psize);
        paramMap.put("Pcount", oracle.jdbc.OracleTypes.INTEGER);
        paramMap.put("v_cur", oracle.jdbc.OracleTypes.CURSOR);
        this.getSqlSession().select("com.gljx.web.dao.ChangeClazzMapper.pagemap", paramMap, null);
        return paramMap;
    }

    /**
     * 取记录总数      dmz 20160921
     *
     * @param psql   SQL语句
     * @param Pcount 记录总数
     * @return
     */
    @Override
    public Map<String, Object> selectPageCount(String psql) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("Psql", psql);
        paramMap.put("Pcount", oracle.jdbc.OracleTypes.INTEGER);
        this.getSqlSession().select("com.gljx.web.dao.ChangeClazzMapper.pagecount", paramMap, null);
        return paramMap;
    }

    /**
     * 保存转班申请
     */
	@Override
	public void insertChangeClazz(ChangeClazz changeClazz) {
		// TODO Auto-generated method stub
		changeClazzMapper.insertSelective(changeClazz);
	}

	/**
	 * 根据ID查询转班申请
	 */
	@Override
	public ChangeClazz selectChangeClazzById(String fid) {
		// TODO Auto-generated method stub
		return  changeClazzMapper.selectChangeClazzById(fid) ;
	}

	/**
	 * 批量删除记录
	 */
	@Override
	public void delChangeClazzById(String[] fids) {
		// TODO Auto-generated method stub
		changeClazzMapper.delChangeClazzById(fids);
	}

	/**
	 * 更新转班申请
	 */
	@Override
	public void updateChangeClazz(ChangeClazz changeClazz) {
		// TODO Auto-generated method stub
		changeClazzMapper.updateByPrimaryKeySelective(changeClazz);
	}


	

	
}
